"""

    Lamptop development utilities
    Copyright (C) 2011  Colby T. Sollars

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
"""

from config import *
import argparse
import sys
import os
from lamptop_action import *

class lamptopApp:
    def __init__(self):
        help_desc = "Lamptop command list:\n"
        help_desc+= "\tcreate_virtualhost\n"
        help_desc+= "\tdatabase\n"
        help_desc+= "\tcreate_project\n"
        help_desc+= "\thelp\n"
        help_desc+= "enter 'lamptop [command] help' to see the usage of each command.'"
        self.arg_parser = argparse.ArgumentParser(
            prog = "lamptop",
            description = help_desc,
            add_help = False
        )
        self.arg_parser.add_argument(
            "command",
            nargs="?",
            default="help"
        )
        self.arg_parser.add_argument(
            "subcommand",
            nargs="?"
        )
        self.arg_parser.add_argument(
            "options",
            nargs="*"
        )
        self.arg_parser.add_argument(
            "--force",
            action="store_true"
        )
        
    def run(self):
        args = sys.argv
        args.pop(0)
        args_parsed = self.arg_parser.parse_args(args)
        
        if show_legal:
            self.show_legal()
        
        if args_parsed.command == "help":
            self.print_help()
            return
        elif args_parsed.command == "create_project":
            action = lamptopCliAction_createProject()
        elif args_parsed.command == "create_virtualhost":
            action = lamptopCliAction_createVirtualhost()
        elif args_parsed.command == "database":
            action = lamptopCliAction_database()
        
        self.sanity_test()
        action.subcommand = args_parsed.subcommand
        action.options = args_parsed.options
        action.force = args_parsed.force
        action.run()
    
    def print_help(self):
        print "usage: lamptop [--force] [command] [subcommand] [options [options ...]]\n"
        print "Lamptop command list:"
        print "\tcreate_virtualhost"
        print "\tdatabase"
        print "\tcreate_project"
        print "\tcreate_help"
        print "\nenter 'lamptop [command] help' to see the usage of each command.'\n"

        print "optional arguments:"
        print "  --force\tWon't ask if you're sure."
    
    def show_legal(self):
        print "\nLamptop Copyright (C) 2011  Colby T. Sollars"
        print "This program comes with ABSOLUTELY NO WARRANTY\n\n"
        
    def sanity_test(self):
        if not os.getuid() == 0:
            print "This application must be run as the user 'root'. Quitting."
            sys.exit()
        if not os.path.exists(host_file_path):
            print "Cannot find host file. Quitting."
            sys.exit()
